# coding: utf-8
import sys, os
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_PATH)# 项目目录
import logging

def init_log(model = logging.INFO):
    # 第一步，创建一个logger
    logger = logging.getLogger()
    logger.setLevel(model)  # Log等级总开关

    # 第二步，创建一个handler，用于写入日志文件
    log_path = BASE_PATH + '/ip_local_data/logs/'
    logfile = log_path + 'run.log'

    fh = logging.FileHandler(logfile, mode='a',encoding="utf-8")
    ch = logging.StreamHandler()
    fh.setLevel(model)  # 输出到file的log等级的开关
    ch.setLevel(model)  # 输出到console的log等级的开关
    # 第三步，定义handler的输出格式
    formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # 第四步，将logger添加到handler里面
    logger.addHandler(fh)
    logger.addHandler(ch)
    # 日志
    # logger.debug('this is a logger debug message')
    # logger.info('this is a logger info message')
    # logger.warning('this is a logger warning message')
    # logger.error('this is a logger error message')
    # logger.critical('this is a logger critical message')
    return logger

logger = init_log()
def debug(str):
    logger.debug(str)
def info(str):
    logger.info(str)
def warning(str):
    logger.warning(str)
def error(str):
    logger.error(str)

# if __name__ == "__main__":
    # info("haha")
    # info("haha")
    # logger.error('this is a logger error message')
